package co.fitstart.entity.market;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import co.fitstart.entity.market.template.MarketDailyCoursewareTemplate;

/**
 * 每天课件
 * 
 * @author dan
 *
 */
@Entity
@Table(name = "market_daily_courseware")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class MarketDailyCourseware {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 状态 */
    private DailyStatus status;
    
    /** 开始运动时间 */
    private Date beginTime;
    
    /** 打卡时间 (结束运动时间)*/
    private Date signinTime;
    
    /** 总运动时间 -- 分钟 */
    private long minutes;
    
    /** 签到名次 */
    private int signinOrder;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private MarketBatchCourseware batch;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private MarketDailyCoursewareTemplate template;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public MarketDailyCourseware() {
        
    }
    
    /**
     * 
     * @param template
     * @param batch
     */
    public MarketDailyCourseware(MarketDailyCoursewareTemplate template, MarketBatchCourseware batch) {
        this.status = DailyStatus.PREPARE;
        this.minutes = 0;
        this.batch = batch;
        this.template = template;
        this.disabled = false;
    }
    
    /**
     * 
     * @param status
     * @return
     */
    public MarketDailyCourseware changeStatus(DailyStatus status) {
        this.status = status;
        return this;
    }
    
    /**
     * 
     * @return
     */
    public MarketDailyCourseware startAction() {
        this.status = DailyStatus.ACTING;
        this.beginTime = new Date();
        return this;
    }
    
    /**
     * 
     * @return
     */
    public MarketDailyCourseware finishAction(int signinOrder) {
        this.status = DailyStatus.FINISHED;
        this.signinTime = new Date();
        this.minutes = (this.signinTime.getTime() - this.beginTime.getTime()) / (1000*60);
        this.signinOrder = signinOrder;
        return this;
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the status
     */
    public DailyStatus getStatus() {
        return status;
    }

    /**
     * @return the beginTime
     */
    public Date getBeginTime() {
        return beginTime;
    }

    /**
     * @return the signinTime
     */
    public Date getSigninTime() {
        return signinTime;
    }

    /**
     * @return the minutes
     */
    public long getMinutes() {
        return minutes;
    }

    /**
     * @return the signinOrder
     */
    public int getSigninOrder() {
        return signinOrder;
    }

    /**
     * @return the batch
     */
    public MarketBatchCourseware getBatch() {
        return batch;
    }

    /**
     * @return the template
     */
    public MarketDailyCoursewareTemplate getTemplate() {
        return template;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(DailyStatus status) {
        this.status = status;
    }

    /**
     * @param beginTime the beginTime to set
     */
    public void setBeginTime(Date beginTime) {
        this.beginTime = beginTime;
    }

    /**
     * @param signinTime the signinTime to set
     */
    public void setSigninTime(Date signinTime) {
        this.signinTime = signinTime;
    }

    /**
     * @param minutes the minutes to set
     */
    public void setMinutes(long minutes) {
        this.minutes = minutes;
    }

    /**
     * @param signinOrder the signinOrder to set
     */
    public void setSigninOrder(int signinOrder) {
        this.signinOrder = signinOrder;
    }

    /**
     * @param batch the batch to set
     */
    public void setBatch(MarketBatchCourseware batch) {
        this.batch = batch;
    }

    /**
     * @param template the template to set
     */
    public void setTemplate(MarketDailyCoursewareTemplate template) {
        this.template = template;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

}
